package exercise7;

public class Exercise12_08 {
    public static void main(String []args) throws HexFormatException {
        System.out.println(hex2Dec("B3"));
        System.out.println(hex2Dec("EE"));
        System.out.println(hex2Dec("R9"));

    }


    public static int hex2Dec(String hexString) throws HexFormatException {
        int value = convertHexToDec(hexString.charAt(0));
        for (int i = 1; i < hexString.length(); i++) {
            value = value * 16 + hexString.charAt(i) - '0';
        }

        return value;
    }

    static int convertHexToDec(char ch) throws HexFormatException {
        if (ch == 'A') {
            return 10;
        }
        else if (ch == 'B') {
            return 11;
        }
        else if (ch == 'C') {
            return 12;
        }
        else if (ch == 'D') {
            return 13;
        }
        else if (ch == 'E') {
            return 14;
        }
        else if (ch == 'F') {
            return 15;
        }
        else if (ch <= '9' && ch >= '0')
            return ch - '0';
        else
            throw new HexFormatException("error: " + ch);
    }
}

class HexFormatException extends Exception{
    public HexFormatException(String s){
        super("不是16进制字符串");
    }
}